package com.atguigu.gmall2020.mock.db.service.impl;

import com.atguigu.gmall2020.mock.db.bean.*;
import com.atguigu.gmall2020.mock.db.mapper.ActivityInfoMapper;
import com.atguigu.gmall2020.mock.db.service.ActivityInfoService;
import com.atguigu.mock.util.RandomNum;
import com.atguigu.mock.util.RandomNumBuilder;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 活动表 服务实现类
 * </p>
 *
 * @author zhangchen
 * @since 2020-02-25
 */
@Service
public class ActivityInfoServiceImpl extends ServiceImpl<ActivityInfoMapper, ActivityInfo> implements ActivityInfoService {


    @Value("${mock.date}")
    String mockDate;

    public void updateRecentlyDate(){
        List<ActivityInfo> activityInfoList = list();

        for (ActivityInfo activityInfo : activityInfoList) {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            Date date =null ;
            Date stDate =null ;
            Date edDate =null ;
            try {
                  date = simpleDateFormat.parse(mockDate);
                  stDate=  DateUtils.addDays(date, 0-(   RandomNum.getRandInt(1,20)));
                  edDate=  DateUtils.addDays(date,  (   RandomNum.getRandInt(1,20)));
            } catch (ParseException e) {
                e.printStackTrace();
            }


            activityInfo.setStartTime(stDate);
            activityInfo.setEndTime(edDate);
        }
        saveOrUpdateBatch(activityInfoList) ;
    }
}
